//
//  ViewController.m
//  iwubi
//
//  Created by betahuang on 2017/12/8.
//  Copyright © 2017年 betahuang. All rights reserved.
//

#import "ViewController.h"
#import "WubiItemTableViewCell.h"
#import "NSString+Chinese.h"
#include "wb_table.h"

@interface WubiItem : NSObject
@property(nonatomic, copy) NSString* ch;
@property(nonatomic, copy) NSString* code;
@property(nonatomic, strong) UIImage* img;
@end

@implementation WubiItem
@end

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textFieldBottomConstraint;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)onHelp:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *ruleImageView;
@property (nonatomic, strong) NSMutableArray *chs;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldTextDidChange)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:self.textField];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.layer.borderWidth = 1.0 / [UIScreen mainScreen].scale;
    self.tableView.layer.borderColor = [UIColor grayColor].CGColor;
    self.tableView.layer.cornerRadius = 3.0;
    
    self.chs = [[NSMutableArray alloc] init];

    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.backgroundColor = [UIColor whiteColor];
    self.textField.textColor = [UIColor blackColor];
    self.textField.layer.borderColor = [UIColor grayColor].CGColor;
    self.textField.layer.borderWidth = 1.0 / [UIScreen mainScreen].scale;
    self.textField.layer.cornerRadius = 3.0;
    [self.textField becomeFirstResponder];
    self.ruleImageView.hidden = YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDarkContent;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)textFieldTextDidChange {
    [self.chs removeAllObjects];
    NSString *input = self.textField.text;
    for (NSInteger index = 0; index < input.length; index++) {
        NSString *ch = [input substringWithRange:NSMakeRange(index, 1)];
        if ([ch isChinese]) {
            WubiItem *item = [[WubiItem alloc] init];
            item.ch = ch;
            NSData *gbk = [ch gbk];
            if (gbk.length == 2) {
                const Byte *bytes = (const Byte *)gbk.bytes;
                unsigned short code = bytes[0];
                code = ( code << 8 ) + bytes[1];
                item.code = [NSString stringWithUTF8String:wubi_code(code)];
                NSURL *imgURL = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"pic/%@", ch] withExtension:@"png"];
                item.img = [UIImage imageWithContentsOfFile:imgURL.path];
                [self.chs addObject:item];
            }
        }
    }
    
    [self.tableView reloadData];
}

-(void) keyboardWillShow:(NSNotification *) note
{
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    [UIView animateWithDuration:[duration doubleValue] animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:[duration doubleValue]];
        [UIView setAnimationCurve:[curve intValue]];
        self.textFieldBottomConstraint.constant = keyboardBounds.size.height + 10;
    }];
}

#pragma mark 键盘隐藏的监听方法
-(void) keyboardWillHide:(NSNotification *) note
{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    [UIView animateWithDuration:[duration doubleValue] animations:^{
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:[duration doubleValue]];
        [UIView setAnimationCurve:[curve intValue]];
        self.textFieldBottomConstraint.constant = 10;
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.chs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WubiItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WubiItemTableViewCell"];
    if (!cell) {
        cell = [[WubiItemTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WubiItemTableViewCell"];
    }
    //自定义cell里面的几个控件内容，self.array[indexPath.row]是一个字典
    WubiItem *item = [self.chs objectAtIndex:indexPath.row];
    cell.codeLabel.text = item.code;
    cell.codeImageView.image = item.img;
    cell.backgroundColor = [UIColor whiteColor];
    cell.codeLabel.textColor = [UIColor blackColor];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
//    return [[UIView alloc] init];
//}
char *wbbm[][3] = {{"1", "23","34"}, {"3", "", ""}};

- (IBAction)onHelp:(id)sender {
    self.ruleImageView.hidden = !self.ruleImageView.hidden;
}
@end
